<?php

namespace App\Http\Resources\orders;

use App\Models\goods\Order;

use Illuminate\Http\Resources\Json\Resource;


class OrderResource extends Resource
{

    public function __construct(Order $resource)
    {
        $this->resource = $resource;
    }

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $details = [];

        foreach ($this->resource->details as $detail){
            $details[] = [
                'id' => $detail->id,
                'goods_id' => $detail->goods_id,
                'type_name' => $detail->type_name,
                'total_price' => $detail->total_price,
                'goods_name' => $detail->goods_name,
                'goods_img' => $detail->goods_img,
                'spec' => $detail->spec??'',
                'num' => $detail->num,
            ];
        }

        return [
            'id' => $this->resource->id,
            'total_price' => $this->resource->total_price,
            'status' => $this->resource->status,
            'created_at' => $this->resource->created_at->toDateTimeString(),
            'status_text' => $this->resource->status_text,
            'is_evaluate' => $this->resource->is_evaluate,
            'details' => $details,
        ];
    }
}
